﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectManagement.Domain.Services;
using ProjectManagement.Domain.Entities;
using ProjectManagement.Web.Infrastructure;

namespace ProjectManagement.Web.Controllers
{
	[Permission(Roles = "系统管理员")]
    public class RoleManageController : Controller
    {
        //
        // GET: /RoleManage/
		private static RoleService _roleService;
		private static ControllerActionRoleService _controllerActionRoleService;
		private static ControllerActionService _controllerActionService;
		public RoleManageController(RoleService roleService,ControllerActionService conaction,ControllerActionRoleService conactionrole)
		{
			_roleService = roleService;
			_controllerActionService = conaction;
			_controllerActionRoleService = conactionrole;
		}
		//角色列表
        public ActionResult RoleList()
        {
			var roles = _roleService.QueryAll();
            return View(roles);
        }

		[HttpPost]
		public ActionResult RoleList(string name, string desption)
		{
			var source=_roleService.QueryAll();
			return View(_roleService.QueryRole(source,name,desption));
		}

		public ActionResult CreateRole()
		{
			ViewBag.AllPermission = _controllerActionService.QueryAllConAction();
			ViewBag.Permission = new List<int> { };
			return View("EditRole", new Role());
		}

		public ActionResult EditRole(int id)
		{
			var role = _roleService.GetRoleById(id);
			var permId = _controllerActionRoleService.QueryControllerIdByRoleId(id);
			List<ControllerAction> list = new List<ControllerAction>();
			foreach (var ids in permId)
			{
				list.Add(_controllerActionService.QueryControllerActionById(ids));
			}
			ViewBag.Permission = list;
			ViewBag.AllPermission = _controllerActionService.QueryAllConAction();
			return View(role);
		}
		[HttpPost]
		public ActionResult EditRole(Role role,FormCollection form)
		{
			if (ModelState.IsValid)
			{
				if (role != null)
				{
					_roleService.SaveOrUpdate(role);
					//原权限集合
					var permId = _controllerActionRoleService.QueryControllerIdByRoleId(role.ID);
					if (form["per"] != null)
					{
						//新权限集合
						var newPermId = form["per"].Split(new char[] { ',' });
						IList<int> permission = new List<int>();
						foreach (var ids in newPermId)
						{
							int id = int.Parse(ids);
							permission.Add(id);
						}
						//需要删除的权限
						var delList = permId.Except(permission);
						foreach(var del in delList)
						{
							_controllerActionRoleService.DeleteConAcRole(role.ID, del);
						}
						//需要添加的权限
						var addList = permission.Except(permId);
						foreach (var add in addList)
						{
							ControllerActionRole con = new ControllerActionRole 
							{
								RoleID=role.ID,
								ControllerActionID=add,
								IsAllowed=true
							};
							_controllerActionRoleService.SaveConActRole(con);
						}
					}
					else
					{
						if (permId.Count>0)
						{
							_controllerActionRoleService.DeleteConActRole(role.ID);
						}
					}
					TempData["Message"] = "修改成功！";
				}
				return RedirectToAction("RoleList");
			}
			else
			{
				return RedirectToAction("EditRole");
			}
		}

		public ActionResult DetailsRole(int id)
		{
			var role = _roleService.Get(id);
			var permId = _controllerActionRoleService.QueryControllerIdByRoleId(id);
			var list = new List<ControllerAction>();
			foreach (var item in permId)
			{
				list.Add(_controllerActionService.QueryControllerActionById(item));
			}
			ViewBag.Permission = list;
			return View(role);
		}

		public ActionResult DeleteRole(int id)
		{
			if (id == 1 || id == 7)
			{
				TempData["Message"] = "禁止删除";
				return RedirectToAction("RoleList");
			}
			else
			{
				var role = _roleService.GetRoleById(id);
				_roleService.Remove(role);
				TempData["Message"] = "删除成功";
				return RedirectToAction("RoleList");
			}
		}
	}
}
